<?php
require_once (APPLICATION_DIR."/application/model/Profile.php");
class Profile extends DataObjects_Profile
{	
	function getProfileInfo($profile_id)
	{
		$this->selectAdd();
		$this->selectAdd("*");
		$this->whereAdd();
		$this->whereAdd("profile_id = $profile_id");
		$this->find();
		return $this;
	}
	
	function getAllProfileTitle()
	{
	    $this->selectAdd();
		$this->selectAdd("profile_id, profile_title, profile_email");
		$this->find();
		return $this;
	}
	
	function getAllProfileList()
	{
	    $this->selectAdd();
		$this->selectAdd("profile_id, profile_title, profile_email, profile_image1");
		$this->find();
		$result = $this->fetchList();
		return $result;
	}
	
	function updateProfileInfo($profile_id,$profile_title,$profile_content)
	{
		$this->profile_id		= $profile_id;
		$this->profile_title 	= $profile_title;
		$this->profile_content  = $profile_content;
		$this->update();
	}
	
	function updateProfileImage($profile_id,$imageArr)
	{
	    $this->profile_id		= $profile_id;
		$this->assignProfileImage($imageArr);
		$this->update();
	}
	
	function assignProfileImage($imageArr)
	{
	    foreach ($imageArr as $key => $value)
	    {
	        if ($value)
	        {
	            switch ($key)
	            {
	                case 0:
	                    $this->profile_image1 = $value;
	                    break;
	                    
	                case 1:
	                    $this->profile_image2 = $value;
	                    break;
	                    
	                case 2:
	                    $this->profile_image3 = $value;
	                    break;
	            }
	        }
	    }
	}
	
	function addProfile($profile_title,$profile_content)
	{
		$this->profile_title 	= $profile_title;
		$this->profile_content  = $profile_content;
		$this->insert();
	}
	
	function deleteProfileImage($profile_id)
	{
	    $this->getProfileInfo($profile_id);
	    $this->fetch();
	    $profile_image[] = $this->profile_image1;
	    $profile_image[] = $this->profile_image2;
	    $profile_image[] = $this->profile_image3;
	    foreach ($profile_image as $key => $value)
	    {
	        if ($value)
	        {
	            @unlink($value);
	            if ($key == 0)
	            {
	                $filepath = explode("/",$value);
	                $filepath[count($filepath)-1] = "thumb_" . $filepath[count($filepath)-1] ;
	                $thumb = implode("/",$filepath);
	                @unlink($thumb);
	            }
	        }
	    }
	}
	
	function deleteProfile($profile_id)
	{
	    $profile = new Profile();
	    $profile->deleteProfileImage($profile_id);
	    $this->profile_id = $profile_id;
	    $this->delete();
	}
	
	function getLastProfileID()
	{
	    $this->selectAdd();
		$this->selectAdd("profile_id");
		$this->orderBy("profile_id DESC");
		$this->find();
		if ($this->fetch())
		{
		    $result = $this->profile_id;
		}
		else 
		{
		    $result = 0;
		}
		return $result;
	}
}
?>